using System;
using System.Collections.Generic;
using Sce.PlayStation.Core;

namespace Nyanimals
{
	public class CAnimatedObject : CMovingObject
	{
		new protected CAnimatedContext context;
		protected Dictionary<string, tFrameLimit> animationLimits;
		
		public CAnimatedObject() : base()
		{
			animationLimits = new Dictionary<string, tFrameLimit>();
		}
		
		public void AddAnimation(tFrameLimit limit)
		{
			animationLimits[limit.Animation] = limit;
		}
		
		public void ChangeAnimation(string szAnim, float fSpeed = 1.0f, bool blend = true, float transTime = 0.1f)
		{
            // make sure we arent already playing and its looping
			tFrameLimit lim = animationLimits[szAnim];
            if(context.CurrentAnimation.Animation == szAnim && lim.Loop && context.Blending == false)
            {
                return;
            }
            
			if(blend)
			{
				context.SetNextAnimation(lim, fSpeed, transTime);
			}
			else
			{
				context.SetCurrentAnimation(lim, fSpeed);
			}
		}
		
		public override int ContextIndex
		{
			get { return contextIndex;}
			set
			{
				contextIndex = value;
				context = (CAnimatedContext)CContextManager.Instance.GetContext(contextIndex);
			}
		}
		
		public new CAnimatedContext Context
		{
			get { return context; }
		}
		
		public override Matrix4 WorldMatrix
		{
			get { return context.WorldMatrix;}
			set { context.WorldMatrix = value;}
		}
		
		public override Vector3 Position
		{
            get 
            {
                position = WorldMatrix.AxisW;
                return position;
            }
            set 
            {
                Matrix4 tempWorld = context.WorldMatrix;
                position = tempWorld.AxisW = value;
                Context.WorldMatrix = tempWorld;
            }
		}
		
		public override ObjectTypes Type
		{
			get { return ObjectTypes.Animated_Object;}
		}
		
		public override void Input()
		{
			base.Input();
		}
		public override void Update(float deltaTime)
		{
			Matrix4 pivotPoint = Matrix4.Translation(Pivot);
			
			context.WorldMatrix = pivotPoint * localMatrix;
			
			context.ApplySkinning(deltaTime, Matrix4.Identity);//, context.WorldMatrix);
		}
		public override bool HandleCollision(ref CBaseObject Obj)
		{
			return base.HandleCollision(ref Obj);
		}
	}
}

